package com.white.mysoundrecorder;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yutf on 2017/8/30 0030.
 */

public class MyDbHelper extends SQLiteOpenHelper
{

    private static final String DB_NAME = "record.db";
    private Context mContext;

    class Column implements BaseColumns
    {
        public static final String TABLE_NAME = "recordings";
        public static final String RECORD_FILE_NAME = "file_name";
        public static final String RECORD_FILE_PATH = "file_path";
        public static final String RECORD_FILE_LENGTH = "file_length";
        public static final String RECORD_FILE_DURATION = "duration";
        public static final String RECORD_FILE_ADD_TIME = "add_time";
    }

    private final String SQL = "create table " + Column.TABLE_NAME
            + " (" + Column._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + Column.RECORD_FILE_NAME + " TEXT,"
            + Column.RECORD_FILE_PATH + " TEXT,"
            + Column.RECORD_FILE_LENGTH + " INTEGER,"
            + Column.RECORD_FILE_DURATION + " INTEGER,"
            + Column.RECORD_FILE_ADD_TIME + " INTEGER);";

    public MyDbHelper(Context context)
    {
        super(context, DB_NAME, null, 1);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    public int getCount()
    {
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query(Column.TABLE_NAME, new String[]{Column._ID}, null,null,null,null,null);
        int size = c.getCount();
        c.close();
        return size;
    }

    public long addItem(RecordItem item)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(Column.RECORD_FILE_ADD_TIME, System.currentTimeMillis());
        cv.put(Column.RECORD_FILE_NAME, item.name);
        cv.put(Column.RECORD_FILE_PATH, item.filePath);
        cv.put(Column.RECORD_FILE_LENGTH, item.fileSize);
        cv.put(Column.RECORD_FILE_DURATION, item.duration);

        return db.insert(Column.TABLE_NAME, null, cv);
    }

    public List<RecordItem> getAll()
    {
        SQLiteDatabase db = getReadableDatabase();
        List<RecordItem> all = new ArrayList<>();
        Cursor c = db.query(Column.TABLE_NAME, null, null,null,null,null,Column.RECORD_FILE_ADD_TIME + " DESC", null);
        if(c.moveToFirst())
        {
            while (c.moveToNext())
            {
                int idix = c.getColumnIndex(Column._ID);
                Long id = c.getLong(idix);
                int nameIx = c.getColumnIndex(Column.RECORD_FILE_NAME);
                String name = c.getString(nameIx);
                int pathIx = c.getColumnIndex(Column.RECORD_FILE_PATH);
                String path = c.getString(pathIx);
                int sizeIx = c.getColumnIndex(Column.RECORD_FILE_LENGTH);
                long size = c.getLong(sizeIx);
                int durationIx = c.getColumnIndex(Column.RECORD_FILE_DURATION);
                long duration = c.getLong(durationIx);
                int timeIx = c.getColumnIndex(Column.RECORD_FILE_ADD_TIME);
                long addtime = c.getLong(timeIx);
                RecordItem r = new RecordItem();
                r.id = id;
                r.name = name;
                r.filePath = path;
                r.duration = duration;
                r.fileSize = size;
                r.addTime = addtime;
                all.add(r);
            }
        }
        c.close();
        return all;
    }

    public void remove(RecordItem item)
    {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(Column.TABLE_NAME, "_ID=?", new String[]{item.id.toString()});
    }

    public void rename(RecordItem item)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(Column.RECORD_FILE_PATH, item.filePath);
        cv.put(Column.RECORD_FILE_NAME, item.name);
        db.update(Column.TABLE_NAME, cv, "_ID=?", new String[]{item.id.toString()});
    }
}
